package com.asm.controller;

import com.alibaba.fastjson.JSONObject;
import com.asm.mapper.SnvCpgAllMapper;
import com.asm.pojo.*;
import com.asm.mapper.CellMarkersMapper;
import com.asm.service.SnvCpgAllService;
import com.asm.util.ExecLinux;
import com.asm.util.FileFr;
import com.asm.util.Path;
import com.asm.util.Shell;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.*;
import java.util.stream.Collectors;

@RestController
@CrossOrigin
public class Analysis3 {
    @Autowired
    private Path path;

    @Autowired
    private ExecLinux execLinux;
    @Autowired
    private SnvCpgAllMapper snvCpgAllMapper;

    @Autowired
    private CellMarkersMapper cellMarkersMapper;

    @GetMapping("/analysis/Prediction/ASM")
    public JSONObject AsmData(@RequestParam("snv") String snv,
                             @RequestParam("tissue") String tissue,
                             @RequestParam("healthStatus") String healthStatus
    ) throws IOException {
        JSONObject result = new JSONObject();
        String[] rsList = snv.split(",");
        System.out.println("snv/ASM: "+snv);
        System.out.println("tissue/ASM: "+tissue);
        System.out.println("healthStatus/ASM: "+healthStatus);
        if (tissue.equals("ALL")){
            tissue = null;
        }
        if(healthStatus.equals("")){
            String[] health = null;
            List<SnvCpgAll> asmData = snvCpgAllMapper.selectAnalysis3AsmByRsList(rsList,tissue,health);
            result.put("asmData",asmData);
        }else {
            String[] health = healthStatus.split(",");
            List<SnvCpgAll> asmData = snvCpgAllMapper.selectAnalysis3AsmByRsList(rsList,tissue,health);
            result.put("asmData",asmData);
        }
        List<Analysis3Gene> geneData = snvCpgAllMapper.getGenesBysnv(Arrays.stream(rsList).distinct().collect(Collectors.toList()));


        result.put("geneData",geneData);

        return result;
    }

    @GetMapping("/analysis/Prediction/gene")
    public JSONObject GeneData(@RequestParam("snv") String snv
    ) throws IOException {
        JSONObject result = new JSONObject();
        String[] rsList = snv.split(",");
        //        去重查询
        List<Analysis3Gene> geneData = snvCpgAllMapper.getGenesBysnv(Arrays.stream(rsList).distinct().collect(Collectors.toList()));
        result.put("geneData",geneData);
        return result;
    }

    @GetMapping("/analysis/Prediction/cellType")
    public JSONObject CellTypeData(@RequestParam("snv") String snv,
                                   @RequestParam("cellType") String cellType
    ) throws IOException {
        JSONObject result = new JSONObject();
        String[] rsList = snv.split(",");
//        System.out.println("genes=====>"+genes);
        if (rsList.length > 0) {
            List<SnvCell> cellMarkers = cellMarkersMapper.getCellMarkers(Arrays.stream(rsList).distinct().collect(Collectors.toList()));
            result.put("cellTypeData", cellMarkers);
        } else {
            result.put("cellTypeData", null);
        }
        return result;
    }

//    废弃
    public List<String> splitGenes(Analysis3Gene analysis3Gene){
        List<String> result = new ArrayList<>();
        if(!analysis3Gene.getAdjacent().equals("NA")){
            result.addAll(Arrays.asList(analysis3Gene.getAdjacent().split(",")));
        }
        if(!analysis3Gene.getDistal().equals("NA")){
            result.addAll(Arrays.asList(analysis3Gene.getDistal().split(",")));
        }
        if(!analysis3Gene.getProximal().equals("NA")){
            result.addAll(Arrays.asList(analysis3Gene.getProximal().split(",")));
        }
        if(!analysis3Gene.getEqtl().equals("NA")){
            result.addAll(Arrays.asList(analysis3Gene.getEqtl().split(",")));
        }
        if(!analysis3Gene.getCpgtarget().equals("NA")){
            result.addAll(Arrays.asList(analysis3Gene.getCpgtarget().split(",")));
        }
        return result;
    }

    public List<SnvGeneCount> getdata(String snv,int count){
        SnvGeneCount snvGeneCount = new SnvGeneCount();
        snvGeneCount.setSnv(snv);
        snvGeneCount.setCount(count);
        List<SnvGeneCount> result = new ArrayList<>();
        result.add(snvGeneCount);
        return result;
    }
}
